<? // vim:set fdm=indent ts=2 ai et sts=2 sw=2 tw=0:
function words_normalize($words) // return array
{
  $subject = words_get_firstword($words,"subject");
  if ($subject) $words=words_remove($words,$subject);
  $verb    = words_get_firstword($words,"verb");
  if ($verb) $words=words_remove($words,$verb);
  $object  = words_get_firstword($words,"object");
  if ($object) $words=words_remove($words,$object);
  $where   = words_get_firstword($words,"where");
  if ($where) $words=words_remove($words,$where);
  $time    = words_get_firstword($words,"time");
  if ($time) $words=words_remove($words,$time);
  $how     = words_get_firstword($words,"how");
  if ($how) $words=words_remove($words,$how);
  return Array($subject,$verb,$object,$where,$time,$how);
}

function words_get_firstword($words,$para)
{
  global $db;
  $found = false;
  mb_internal_encoding("UTF-8");
  $len  = mb_strlen($words);
  $word = "";
  for ($i=$len;$i>0;$i--)
  {
    $word = mb_substr($words,0,$i);
    $sql  = "SELECT * FROM `word$para` WHERE word='$word' LIMIT 1";
    // 還有相關連沒做
    $data = $db->SQL($sql);
    if (count($data)>=1) { $found = true; break; }
  }
  return ($found)?$word:NULL;
}

function words_remove($words,$str)
{
  $strlength = strlen($str);
  $wordslen  = strlen($word);
  return substr($words,$strlength,$wordslen-$strlength);
}
?>
